package com.eulog.client.blackJack;

import java.util.ArrayList;

import com.eulog.client.blackJack.BlackJackStrategy.SD;
import com.eulog.client.trump.TrumpCard;
import com.eulog.client.trump.TrumpHandCards;

public class BlackJackHandCards extends TrumpHandCards{

	private double additionalBet;
	private ArrayList<BlackJackStrategy.SD> decisionList;
	
	public BlackJackHandCards() {
		super();
		additionalBet = 0;
		decisionList = new ArrayList<SD>();
	}
	
	public BlackJackHandCards(TrumpCard c) {
		super(c);
		additionalBet = 0;
		decisionList = new ArrayList<SD>();
	}
	
	@Override
	public String toString() {
		
		return super.toString() + " {" + toStringDecisionList() + "}";
	}
	
	public String toStringDecisionList() {
		String name ="";
		
		for (SD e: decisionList) {
			if(e!=null)	name += e;
		}
		return name;
	}
	
	@Override
	public void clear() {
		handCards.clear();
		additionalBet = 0;
		decisionList.clear();
	}
	

	public boolean isBusted() {
		if(calculateRanks() > 21) return true;
		return false;
	}

	public boolean isBlackJack() {
		if(calculateRanks() == 21 && handCards.size()==2) return true;
		return false;
	}

	
	public int calculateRanks() {
		int totalAce = 0;
		int totalRank = 0;
		for (TrumpCard c : handCards) {
			if(c.isAce()) totalAce++;
			totalRank+=BlackJack.evaluateRank(c);
		}
		
		while(totalAce>0 && totalRank>21) {
			totalRank-=10;
			totalAce--;
		}
		
		return totalRank;
	}
	
	public int calculateRanksWithOneMoreCard(TrumpCard card) {
		int totalRank = calculateRanks();
		totalRank+=BlackJack.evaluateRank(card);
		if(card.isAce() && totalRank>21) totalRank-=10;
		
		return totalRank;
	}
	
	public int calculateRanksAllAceAreOneValue() {
		int totalRank = 0;
		int eval;
		for (TrumpCard c : handCards) {
			eval=BlackJack.evaluateRank(c);
			if(eval==11) eval = 1;
			totalRank+=eval;
		}
		
		return totalRank;
	}
	
	public boolean isSoft() {
		if(calculateRanks() != calculateRanksAllAceAreOneValue()) return true;
		return false;
	}
	
	public int calculateRanksWithoutOneAceAndOtherAceIsOneValue() {
		int totalAce = 0;
		int totalRank = 0;
		for (TrumpCard c : handCards) {
			if(c.isAce()) totalAce++;
			totalRank+=BlackJack.evaluateRank(c);
		}
		
		if(totalAce>0) totalRank--; // calculate without one Ace
		
		while(totalAce>0) {
			totalRank-=10;
			totalAce--;
		}
		
		return totalRank;
	}
	
	public int cardSizeByEvaluation(int eval) {
		int count=0;
		
		for (TrumpCard c : handCards) {
			if(BlackJack.evaluateRank(c) == eval) count++;
		}
		
		return count;
	}
	
	public void addAddtionalBet(double bet) {additionalBet+= bet;}
	public double getAdditionalBet() {return additionalBet;}
	public boolean canDoublingDown() {if(handCards.size() == 2) return true; return false;}
	
	public void addDecision(BlackJackStrategy.SD d) {decisionList.add(d);} 
	
	public boolean isTwoCardsSameRank() {
		if(handCards.size()==2) {
			if(BlackJack.evaluateRank(handCards.get(0)) == BlackJack.evaluateRank(handCards.get(1))) return true;
		}
		
		return false;
	}
}
